<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 栈的特点是后入先出
        class Stack {
            constructor() {
                this.items = [];
            };

            // 从后面添加元素
            push(ele) {
                this.items.push(ele);
            }

            // 删除最后一个元素
            pop() {
                return this.items.pop();
            }

            // 返回最后一个值
            peek() {
                return this.items[this.items.length - 1];
            }

            // 判断是否为空
            isEmpty() {
                return this.items.length == 0;
            }

            // 清空栈
            clear() {
                this.items = [];
            }

            // 输出栈中数值
            print() {
                console.log(this.items.toString());
            }
        }

        // 十进制转换为二进制

        function transToTwo(num) {
            let stack = new Stack(),
                remainder = 0,
                string = '';

            while (num > 0) {
                remainder = Math.floor(num % 2);
                stack.push(remainder);
                num = Math.floor(num / 2);
            }

            while (!stack.isEmpty()) {
                string += stack.pop().toString();
            }

            return string;
        }

        console.log(transToTwo(10));
    </script>
</body>

</html>